const withLess = require("@zeit/next-less");
const withCss = require("@zeit/next-css");
const withPlugins = require("next-compose-plugins");
const cssLoaderGetLocalIdent = require("css-loader/lib/getLocalIdent.js");
const path = require("path");
const fs = require("fs");
const lessToJS = require("less-vars-to-js");
const FilterWarningsPlugin = require("webpack-filter-warnings-plugin");

const isProd = process.env.APP_ENV === "prod";
 
const BASE_PATH = '';
function getBasePath() {
  let basePath = "";
  if (isProd) {
    basePath = BASE_PATH;
  }
  return basePath;
}

module.exports = withPlugins([withLess, withCss], {
  distDir: "build",
  assetPrefix: getBasePath(), //加前缀
  basePath: getBasePath(), //node
  publicRuntimeConfig: { 
    APP_ENV: process.env.APP_ENV,
    basePath: getBasePath(), //写入路径
  },
  lessLoaderOptions: {
    //如果是antd就需要，antd-mobile不需要
    javascriptEnabled: true,
    modifyVars: lessToJS(
      fs.readFileSync(path.resolve(__dirname, "./static/styles/antd.less"), "utf8")
    ),
  },
  // cssModules: true,
  // cssLoaderOptions: {
  //   camelCase: true,
  //   localIdentName: "[local]___[hash:base64:5]",
  //   getLocalIdent: (context, localIdentName, localName, options) => {
  //     let hz = context.resourcePath.replace(context.rootContext, "");
  //     if (/node_modules/.test(hz)) {
  //       return localName;
  //     } else {
  //       return cssLoaderGetLocalIdent(
  //         context,
  //         localIdentName,
  //         localName,
  //         options
  //       );
  //     }
  //   },
  // },
  webpack(config, { isServer }) {
    // Fixes npm packages that depend on `fs` module
    if (!isServer) {
      config.node = {
        fs: "empty",
      };
    }
    if (config.externals) {
      const includes = [/antd/];
      config.externals = config.externals.map((external) => {
        if (typeof external !== "function") return external;
        return (ctx, req, cb) => {
          return includes.find((include) =>
            req.startsWith(".")
              ? include.test(path.resolve(ctx, req))
              : include.test(req)
          )
            ? cb()
            : external(ctx, req, cb);
        };
      });
    }
    config.resolve.alias = {
      ...config.resolve.alias,
      '@': path.resolve(__dirname),
    };
    config.plugins.push(
      new FilterWarningsPlugin({
        exclude: /mini-css-extract-plugin[^]*Conflicting order between:/,
      })
    );
    config.module.rules.push({
      test: /\.svg/,
      use: [
        {
          loader: "svg-url-loader",
          options: {
            // make all svg images to work in IE
            iesafe: true,
          },
        },
      ],
    });
    return config;
  },
});
